// pages/order/order.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeTab: 0,
        statusTabs: ['全部', '待付款', '待使用', '已完成', '退款/取消'],
        orders: [{
                id: 1,
                payType: 1,
                statusText: '待付款',
                createTime: '2023-12-01',
                totalAmount: 299.00,
                description: '1房·可住两人·一床',
                startDate: '2025-07-10',
                endDate: '2025-07-12',
                cover: 'https://image.shengxuankj.com/fronts/roomList01.png',
            },
            {
                id: 2,
                payType: 1,
                statusText: '待付款',
                createTime: '2023-12-01',
                totalAmount: 299.00,
                description: '1房·可住两人·一床',
                startDate: '2025-07-10',
                endDate: '2025-07-12',
                cover: 'https://image.shengxuankj.com/fronts/roomList02.png',
            },
            {
                id: 2,
                payType: 1,
                statusText: '待付款',
                createTime: '2023-12-01',
                totalAmount: 299.00,
                description: '1房·可住两人·一床',
                startDate: '2025-07-10',
                endDate: '2025-07-12',
                cover: 'https://image.shengxuankj.com/fronts/roomList01.png',
            },
        ],
        filteredOrders: [],
        loading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            filteredOrders: this.data.orders
        })
    },

    /**
     * 切换状态标签
     */
    onTabChange(event) {
        const {
            index
        } = event.detail
        this.setData({
            activeTab: index
        })
        this.filterOrders(index)
    },

    /**
     * 筛选订单
     */
    filterOrders(tabIndex) {
        const {
            statusTabs,
            orders
        } = this.data
        const status = statusTabs[tabIndex]

        let filteredOrders = orders
        if (status !== '全部') {
            const statusMap = {
                '待付款': 'pending_payment',
                '待发货': 'pending_shipment',
                '待收货': 'shipped',
                '已完成': 'completed'
            }
            filteredOrders = orders.filter(order => order.status === statusMap[status])
        }

        this.setData({
            filteredOrders
        })
    },

    /**
     * 查看订单详情
     */
    goToOrderDetail(event) {
        const {
            id
        } = event.currentTarget.dataset
        wx.navigateTo({
            url: `/pages/order-detail/order-detail?id=${id}`
        })
    },

    /**
     * 立即付款
     */
    payOrder(event) {
        const {
            id
        } = event.currentTarget.dataset
        wx.showModal({
            title: '确认支付',
            content: '确定要支付这个订单吗？',
            success: (res) => {
                if (res.confirm) {
                    this.processPayment(id)
                }
            }
        })
    },

    /**
     * 处理支付
     */
    processPayment(orderId) {
        wx.showLoading({
            title: '支付中...'
        })

        setTimeout(() => {
            wx.hideLoading()
            wx.showToast({
                title: '支付成功',
                icon: 'success'
            })

            // 更新订单状态
            const orders = this.data.orders.map(order => {
                if (order.id === orderId) {
                    return {
                        ...order,
                        status: 'pending_shipment',
                        statusText: '待发货'
                    }
                }
                return order
            })

            this.setData({
                orders
            })
            this.filterOrders(this.data.activeTab)
        }, 2000)
    },

    /**
     * 确认收货
     */
    confirmReceipt(event) {
        const {
            id
        } = event.currentTarget.dataset
        wx.showModal({
            title: '确认收货',
            content: '确定已收到商品吗？',
            success: (res) => {
                if (res.confirm) {
                    // 更新订单状态
                    const orders = this.data.orders.map(order => {
                        if (order.id === id) {
                            return {
                                ...order,
                                status: 'completed',
                                statusText: '已完成'
                            }
                        }
                        return order
                    })

                    this.setData({
                        orders
                    })
                    this.filterOrders(this.data.activeTab)

                    wx.showToast({
                        title: '确认收货成功',
                        icon: 'success'
                    })
                }
            }
        })
    },

    /**
     * 查看物流
     */
    viewTracking(event) {
        const {
            tracking
        } = event.currentTarget.dataset
        wx.showModal({
            title: '物流信息',
            content: `快递单号：${tracking}\n\n商品正在配送中，请耐心等待...`,
            showCancel: false
        })
    },

    /**
     * 删除订单
     */
    deleteOrder(event) {
        const {
            id
        } = event.currentTarget.dataset
        wx.showModal({
            title: '删除订单',
            content: '确定要删除这个订单吗？',
            success: (res) => {
                if (res.confirm) {
                    const orders = this.data.orders.filter(order => order.id !== id)
                    this.setData({
                        orders
                    })
                    this.filterOrders(this.data.activeTab)

                    wx.showToast({
                        title: '订单已删除',
                        icon: 'success'
                    })
                }
            }
        })
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh() {
        this.setData({
            loading: true
        })

        setTimeout(() => {
            this.setData({
                loading: false
            })
            wx.stopPullDownRefresh()
            wx.showToast({
                title: '刷新成功',
                icon: 'success'
            })
        }, 1000)
    },

    /**
     * 返回上一页
     */
    goBack() {
        wx.navigateBack()
    },

    goToOrderDetail(){
        wx.navigateTo({
          url: '/packageA/orderDetail/orderDetail',
        })
    }
})